package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_DEAL_APPLY, desc = "处理加入申请")
public class DealJoinApplyCmd extends AbstractCommand {

	private boolean isAllRefuse;

	private long applyUserId;

	private boolean isAgree;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionDealJoinApplyReqMsg reqMsg = UnionProto.UnionDealJoinApplyReqMsg.parseFrom(packet.getBytes());
		isAllRefuse = reqMsg.getIsAllRefuse();
		applyUserId = reqMsg.getUserId();
		isAgree = reqMsg.getIsAgree();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		// 判断权限 族长 副族长才有权限
		String unionUid = player.getUserInfo().getUnionUid();
		if(StringUtils.isNullOrEmpty(unionUid)){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UnionMember member = CrossUnionMgr.getUnionMember(unionUid, player.getUserId());
		if(null == member){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		if(!SeacraftSouthSeaMgr.canInAndOutUnion(unionUid)){
			ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_CAN_NOT_JOIN;
			return;
		}
		// 统兵演武加入商会限制
		if (!TongBingMgr.canChangeUnion()) {
			ret = GameErrorCode.E_TONG_BING_CAN_NOT_OPERATE_UNION;
			return;
		}
		if (!TonBingMgr.canChangeUnion()) {
			ret = GameErrorCode.E_TON_BING_CAN_NOT_OPERATE_UNION;
			return;
		}
		if (!ChannelTonBingMgr.canChangeUnion()) {
			ret = GameErrorCode.E_CTON_BING_CAN_NOT_OPERATE_UNION;
			return;
		}
		if (!FiveHeroMgr.canChangeUnion()) {
			ret = GameErrorCode.E_FIVE_HERO_CAN_NOT_OPERATE_UNION;
			return;
		}
		if(!RankUnionWarMgr.canJoinUnion(unionUid)){
			ret = GameErrorCode.E_RANKUNIONWAR_ACTIVITY_CAN_NOT_JOIN;
			return;
		}

		if(isAgree && !PeakStrongholdActivityMgr.canChangeUnion(unionUid)){
			ret = GameErrorCode.E_PEAK_HOLD_CAN_NOT_JOIN_UNION;
			return;
		}
		if(!PeakSeacraftMgr.canInAndOutUnion(unionUid)){
			ret = GameErrorCode.E_PEAK_SEACRAFT_CAN_NOT_JOIN;
			return;
		}
		UnionProto.UnionDealJoinApplyReqMsg.Builder reqMsg = UnionProto.UnionDealJoinApplyReqMsg.newBuilder();
		reqMsg.setIsAllRefuse(isAllRefuse);
		reqMsg.setUserId(applyUserId);
		reqMsg.setIsAgree(isAgree);
		player.sendPacket(Protocol.C_UNION_DEAL_APPLY, reqMsg);

	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0) {
			UnionProto.UnionDealJoinApplyRespMsg.Builder respMsg = UnionProto.UnionDealJoinApplyRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_UNION_DEAL_APPLY, respMsg);
		}
	}

}
